<#--<#include "../layout/base.ftl">-->
<#--<#macro header>-->
<style type="text/css">
    #subject_search_panel span.label {
        margin-left: 10px;
    }

    #subject_search_panel .input-text {
        border: 1px solid #6D869E;
        height: 18px;
        vertical-align: middle;
        width: 180px;
    }

    #subject_search_panel span.om-combo, #subject_search_panel span.om-calendar {
        vertical-align: middle;
    }

    .form {
        float: left;
        width: 100%;
        text-align: left;
        border-collapse: collapse;
        background: #fff;
    }

    .form caption {
        font-size: 16px;
        font-weight: bold;
        letter-spacing: 2px;
        color: #19609d
    }

    .form th {
        text-align: right;
        font-size: 14px
    }

    .form tr, .form td {
        padding-left: 3px;
        height: 25px
    }

    .form a:link, .form a:visited {
        color: #246ab5;
        text-decoration: underline
    }

    .form a:hover, .form a:active {
        text-decoration: underline
    }

    .se {
        background: #fafbfe
    }

    .outtable {
        border-collapse: collapse;
    }

    .outtable th {
        border: 1px #d9dde1 solid;
        font-weight: normal;
        color: #173d72;
        text-align: left;
        text-indent: 6px;
        background: #fafbfe;
    }

    .outtable td {
        border: 1px #d9dde1 solid;
        padding: 3px;
        font-size: 14px;
        background: #fff
    }

    .errorImg {
        background: url("${base}/css/default/images/msg_bg.png") no-repeat scroll 0 0 transparent;
        height: 16px;
        width: 16px;
        cursor: pointer;
    }

    input.error, textarea.error {
        border: 1px solid red;
    }

    .errorMsg {
        border: 1px solid gray;
        background-color: #FCEFE3;
        display: none;
        position: absolute;
        margin-top: -18px;
        margin-left: 18px;
    }

</style>
<script type="text/javascript">
    var companyList = [
    <#if companyList??&&companyList?size&gt;0>
        <#list companyList as company>
            {text: "${company[2]!""}", value: "${company[0]!""}"}<#if company_has_next>,</#if>
        </#list>
    </#if>
    ];

    $(function () {
        $("#subject_search_panel").omPanel({
            title: "高级搜索"
        });

        $('span#subject_button_search').omButton({
            width: 70,
            icons: {left: '${base}/css/default/images/advanced/search.png'},
            onClick: function (event) {
                var url = "${base}/cfg/common_subject_grid_json?companyId=" + $("#csm_company_name").omCombo("value");
                $('#subject_grid').omGrid("setData", encodeURI(url));
            }
        });

        $("#csm_company_name").omCombo({
            dataSource: companyList,
            listAutoWidth: true,
            filterStrategy: 'anywhere',
            editable: false
        });


        $('#subject_button_bar').omButtonbar({
            width: '100%',
            btns: [
                {id: "add_subject",
                    icons: {left: '${base}/css/default/images/edit_add.png'},
                    label: "保存",
                    onClick: function () {
                        var data = "[";
                        var rows = $('#subject_grid').omGrid('getData').rows;
                        for (var i = 0; i < rows.length; i++) {
                            var temp = $("input[name='code']")[i].value;
                            data += '{"codeId":"' + rows[i].codeId + '","companyId":"' + rows[i].companyId + '","code":"' + temp + '","deptId":null}';
                            data += ','
                        }
                        if (data.length > 1)
                            data = data.substring(data, data.length - 1) + ']';
                        $.post('${base}/cfg/save_subject_grid', {'data': data}, function (data) {
                        <#--var url = "${base}/cfg/common_subject_grid_json?companyId=" + $("#csm_company_name").omCombo("value");-->
                        <#--$('#subject_grid').omGrid("setData", encodeURI(url));-->
                            $('span#rp_button_search').omButton("click");
                            var type = data.success ? "success" : "error";
                            $.omMessageBox.alert({
                                type: type,
                                title: '提示',
                                content: data.message
                            });
                        });
                    }
                }
            ]
        });


        var url = "${base}/cfg/common_subject_grid_json";

        var grid = $('#subject_grid').omGrid({
            method: 'POST',
            limit: 0,
            dataSource: encodeURI(url),
            height: 380,
            width: 'fit',
            autoFit: true,
            colModel: [
                {header: '公司名称', name: 'companyName', align: 'left'},
                {header: '原U8科目名称', name: 'U8SubjectName', align: 'left'},
                {header: '久其科目名称', name: 'subjectName', align: 'left'},
                {header: '久其科目代码', name: 'code', align: 'center', renderer: function (colValue, rowData, rowIndex) {
                    var data = rowData;
                    return '<input name="code" type="text"  value="' + colValue + '"></input>';
                }},
                {header: '单位权限', name: 'deptId', align: 'left'}
            ]
        });

        $(window).scroll(function () {
            $("#subject_grid").omGrid('resize');
        });

        $('.errorImg').bind('mouseover',function () {
            $(this).next().css('display', 'block');
        }).bind('mouseout', function () {
                    $(this).next().css('display', 'none');
                });

        $("#subject_dialog").omDialog({
            title: "新增供应商映射",
            autoOpen: false,
            resizable: true,
            modal: true,
            width: 460,
            onOpen: function (event) {
                $("#smd_company_name").omCombo("value", "");
                $("#smd_subject_name").val("");
                $("#smd_subject_id").val("");
                $("#smd_code").val("");

                var remote = $("#subject_dialog_from").validate({
                    rules: {
                        companyId: { required: true},
                        subjectId: { required: true},
                        code: { required: true}
                    },
                    messages: {
                        companyId: {required: "请选择公司"},
                        subjectId: {required: "请选择供应商"},
                        code: { required: "请输入代码"}
                    },
                    errorPlacement: function (error, element) {
                        if (error.html()) {
                            $(element).parents().map(function () {
                                if (this.tagName.toLowerCase() == 'td') {
                                    var attentionElement = $(this).next().children().eq(0);
                                    attentionElement.css('display', 'block');
                                    attentionElement.next().html(error);
                                }
                            });
                        }
                    },
                    showErrors: function (errorMap, errorList) {
                        if (errorList && errorList.length > 0) {
                            $.each(errorList, function (index, obj) {
                                var msg = this.message;
                                $(obj.element).parents().map(function () {
                                    if (this.tagName.toLowerCase() == 'td') {
                                        var attentionElement = $(this).next().children().eq(0);
                                        attentionElement.show();
                                        attentionElement.next().html(msg);
                                    }
                                });
                            });
                        } else {
                            $(this.currentElements).parents().map(function () {
                                if (this.tagName.toLowerCase() == 'td') {
                                    $(this).next().children().eq(0).hide();
                                }
                            });
                        }
                        this.defaultShowErrors();
                    },
                    submitHandler: function () {
                        $('#subject_dialog_from').omAjaxSubmit({success: function (data, statusText, xhr, $form) {
                            if (data.success == false)
                                alert(data.message);
                            else {
                                $("#subject_dialog").omDialog("close");
                                $("#subject_grid").omGrid('reload', 1);
                            }
                        }});
                        return false;
                    }
                });
            },
            buttons: [
                {
                    text: "确定",
                    click: function () {
                        $("#subject_dialog_from").submit();
                    }
                },
                {
                    text: "取消",
                    click: function () {
                        $("#subject_dialog").omDialog("close");
                    }
                }
            ]
        });
    });
</script>
<#--</#macro>-->
<#--<#macro body>-->
<div id="subject_search_panel">
    <div style="width: 800px;">
        <span class="label">公司名称：</span>
        <input id="csm_company_name"/>
        <span id="subject_button_search">搜索</span>
    </div>
</div>
<div id="subject_button_bar"/>
<table id="subject_grid"></table>

<div id="subject_dialog">
    <form id="subject_dialog_from" action='${base}/cfg/save_subject_mapping' method='post'>
        <table width="100%" class="form outtable">
            <tr>
                <th>公司名称</th>
                <td><input id="smd_company_name" name="companyId"/></td>
                <td><span class="errorImg"></span><span class="errorMsg"></span></td>
            </tr>
            <tr>
                <th>供应商</th>
                <td>
                    <input id="smd_subject_name" name="smd_subject_name"/>
                    <input id="smd_subject_id" name="subjectId" type="hidden"/>
                </td>
                <td width="50"><span class="errorImg"></span><span class="errorMsg"></span></td>
            </tr>
            <tr>
                <th>久其供应商代码</th>
                <td>
                    <input id="smd_code" name="code"/>
                </td>
                <td><span class="errorImg"></span><span class="errorMsg"></span></td>
            </tr>
        </table>
    </form>
</div>
<#--</#macro>-->